package com.jdk.thread.lock2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * @Description :
 * @FileName: ThreadTest1.java
 * @Author :WeiHui.Zhang
 * @Data : 2016年2月1日 下午2:55:00
 * @Version:V1.00
 */
public class ThreadTest2 extends Thread {

	private int threadNo;
	private String lock;

	public ThreadTest2(int threadNo, String lock) {
		this.threadNo = threadNo;
		this.lock = lock;
	}

	@Override
	public void run() {
		synchronized (lock) {
			try {
				FileOutputStream fileOutputStream = new FileOutputStream("C:/" + threadNo + ".txt");
				for (int i = 1; i < 10000; i++) {
					String data = Thread.currentThread().getName() + "-No." + threadNo + ":" + i;
					PrintStream out = new PrintStream(fileOutputStream);
					System.setOut(out);
					System.out.println(data);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

	}

	public static void main(String[] args) throws InterruptedException {
		for (int i = 0; i < 10; i++) {
			new ThreadTest2(i, "lock").start();
			Thread.sleep(1);
		}
	}
}
